<?php
/**
 * Created by PhpStorm.
 * User: Andy
 * Date: 2019/3/20
 * Time: 11:03
 */

namespace App\api\controller;


use Common\controller\Controller;
use Curl;

/**
 * 中国移动-订单小号接口对接
 * Class CMCCCall
 * @package App\api\controller
 */
class CMCCCall  extends Controller
{
    private static $plantFormId='';
    private static $apiKey='eaeddfc36a3e4b7b851c516b268a56c4';
    private static $secretKey='077adca8dd424b78a558037f0f6a5809';
    private static $bindUrl='http://dev.cmccopen.cn/api/v1/mid/bind'; //绑定
    private static $unBindUrl='http://dev.cmccopen.cn/api/v1/mid/unbind'; //解绑


    /**绑定 小号x
     * @throws \ErrorException
     */
    public function bindX(){
        $Authorization=$this->createAuthorization();
        $accessCode=$this->createAccessCode();

        $headers=[
            'Content-Type: application/json',
            'charset=UTF-8',
            "Authorization: $Authorization",
            "AccessCode: $accessCode"
        ];
        $data=[
            'telA'=>$_REQUEST['telA'],
            'telX'=>$_REQUEST['telX'],
            'areaCode'=>'',
            'expiration'=>$_REQUEST['expiration'], //s
            "record"=>"0",
            "gnFlag"=>"00"
        ];

        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_HEADER, FALSE);
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
        $curl->setOpt(CURLOPT_AUTOREFERER, 1);
        $curl->setOpt(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $curl->setOpt(CURLOPT_HTTPHEADER, $headers);

        $curl->post(self::$bindUrl,$data);
        if ($curl->error) {
            echo $curl->error_code;
        }

        else {
            echo $curl->response;
        }
    }

    /**
     * 解绑
     */
    public function unbind(){
        $Authorization=$this->createAuthorization();
        $accessCode=$this->createAccessCode();

        $headers=[
            'Content-Type: application/json charset=UTF-8',
            "Authorization: $Authorization",
            "AccessCode: $accessCode"
        ];
        $data=[
            'bindId'=>$_REQUEST['bindId'], //小号id
            'coolDown'=>$_REQUEST['coolDown'], //冷却天数
        ];

        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_HEADER, FALSE);
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
        $curl->setOpt(CURLOPT_AUTOREFERER, 1);
        $curl->setOpt(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $curl->setOpt(CURLOPT_HTTPHEADER, $headers);

        $curl->post(self::$unBindUrl,$data);
        if ($curl->error) {
            echo $curl->error_code;
        }

        else {
            echo $curl->response;
        }

    }
    /**
     * 生成Authorizatio
     */
    public function createAuthorization(){
        $sign=md5(self::$apiKey.self::$secretKey.time());
        $data=[
            'apiKey'=>self::$apiKey,
            'sign'=>$sign,
            'time'=>time(),
        ];

        $data=json_encode($data);
        return base64_encode($data);
    }

    /**
     * 生成  AccessCode
     */
    public function createAccessCode(){
        $data=[
            'platformId'=>self::$plantFormId,
            'secret'=>self::$secretKey,
        ];
        return base64_encode(json_encode($data));
    }





}